package com.miracle9.game.server.message;

import com.miracle9.game.util.HexUtil;
import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.Map;

public class XsSocketHandlerTest implements IoHandler {

    private Logger logger = Logger.getLogger(XsSocketHandlerTest.class);
    @Override
    public void sessionCreated(IoSession ioSession) throws Exception {
        logger.info("xs test socket success");
    }

    @Override
    public void sessionOpened(IoSession ioSession) throws Exception {

    }

    @Override
    public void sessionClosed(IoSession ioSession) throws Exception {
        this.logger.info("sessionClosed2...");
    }

    @Override
    public void sessionIdle(IoSession ioSession, IdleStatus idleStatus) throws Exception {

    }

    @Override
    public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {

    }

    @Override
    public void messageReceived(IoSession ioSession, Object o) throws Exception {
        IoBuffer in = (IoBuffer) o;
        String code  = in.getHexDump();
        logger.info("request info2222222222222:" + code);
        if(!MainServer.yjsocket.isEmpty()){
            byte[] data = HexUtil.hexStringToByteArray(code);
            //      byte[] content = code.getBytes("UTF-8");
            IoBuffer bb = IoBuffer.allocate(data.length);
            bb.put(data, 0, data.length);
            bb.flip();
           // ioSession.write(bb);
            for (Map.Entry<String, IoSession> entry :  MainServer.yjsocket.entrySet()) {
                entry.getValue().write(bb);
            }

        }
    }

    @Override
    public void messageSent(IoSession ioSession, Object o) throws Exception {

    }
}
